package com.lmh.controller;

import com.lmh.entity.User;
import com.lmh.framework.jwt.JWTUtil;
import com.lmh.service.UserService;
import com.lmh.utils.Maps;
import com.lmh.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public Result login(@RequestBody Map<String, String> map) {
        String userName = map.get("userName");
        String password = map.get("password");
        User user = userService.login(userName, password);
        if(user != null) {
            String token = JWTUtil.sign(user);
            return Result.ok(Maps.build().add("token", token).add("user", user).getParamMap());
        } else {
            return Result.fail("用户或密码错误");
        }
    }
}
